//
//  LYQHomeViewController.m
//  ZCProject
//
//  Created by Zheng on 16/11/6.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQHomeViewController.h"

#import "CXCycleView.h"
#import "LYQHomeSoftCell.h"
#import "LYQHomeTourismCell.h"
#import "LYQHomeRecruitCell.h"
#import "LYQHomeResourceCell.h"
#import "LYQHomeAPICell.h"
#import "LYQHomePublicationView.h"

#define kLYQHomeSoftCell_ID @"kLYQHomeSoftCell_ID"
#define kLYQHomeTourismCell_ID @"kLYQHomeTourismCell_ID"
#define kLYQHomeRecruitCell_ID @"kLYQHomeRecruitCell_ID"
#define kLYQHomeResourceCell_ID @"kLYQHomeResourceCell_ID"
#define kLYQHomeAPICell_ID @"kLYQHomeAPICell_ID"

@interface LYQHomeViewController () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) CXCycleView *bannerHeaderView;
@property (nonatomic, strong) NSArray *homeSoftTitlesArr;

@property (nonatomic, strong) UIButton *toSearchBtn; //搜索条按钮
@property (nonatomic, strong) UITextField *searchTxf;

@property (nonatomic, strong) UIBarButtonItem *rightItem; //发布按钮

@property (nonatomic, strong) LYQHomePublicationView *publicationView; //发布视图

@end

@implementation LYQHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    [self.listTableView registerClass:[LYQHomeSoftCell class] forCellReuseIdentifier:kLYQHomeSoftCell_ID];
    [self.listTableView registerClass:[LYQHomeTourismCell class] forCellReuseIdentifier:kLYQHomeTourismCell_ID];
    [self.listTableView registerClass:[LYQHomeRecruitCell class] forCellReuseIdentifier:kLYQHomeRecruitCell_ID];
    [self.listTableView registerClass:[LYQHomeResourceCell class] forCellReuseIdentifier:kLYQHomeResourceCell_ID];
    [self.listTableView registerClass:[LYQHomeAPICell class] forCellReuseIdentifier:kLYQHomeAPICell_ID];
    
    _homeSoftTitlesArr = @[@"旅业商情",@"热门招聘",@"最新资源"];
    
    [self.navigationController.navigationBar addSubview:self.toSearchBtn];

    self.navigationItem.rightBarButtonItem = self.rightItem;
     
    [[UIApplication sharedApplication].keyWindow addSubview:self.publicationView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];

    _toSearchBtn.hidden = NO;
    
    [_publicationView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.bottom.equalTo([UIApplication sharedApplication].keyWindow);
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    _toSearchBtn.hidden = YES;
}

#pragma mark - load subviews
- (UIButton *)toSearchBtn {
    if (!_toSearchBtn) {
        _toSearchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _toSearchBtn.backgroundColor = RGB(236, 236, 236, 1);
        _toSearchBtn.frame = CGRectMake(20, 7, kMS_Width-110, 30);
        _toSearchBtn.layer.cornerRadius = 15;
        [_toSearchBtn setImage:[UIImage imageNamed:@"searchicon"] forState:UIControlStateNormal];
        [_toSearchBtn setTitle:@"找资源, 找供应商" forState:UIControlStateNormal];
        [_toSearchBtn setTitleColor:kLYQGray forState:UIControlStateNormal];
        _toSearchBtn.titleLabel.font = kFont(13);
        _toSearchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _toSearchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        _toSearchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        if (!_searchTxf) {
            _searchTxf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, kMS_Width-110, 30)];
            _searchTxf.tintColor = [UIColor clearColor];
            _searchTxf.delegate = self;
        }
        [_toSearchBtn addSubview:_searchTxf];
    }
    return _toSearchBtn;
}

- (UIBarButtonItem *)rightItem {
    if (!_rightItem) {
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [rightBtn setTitle:@"发布" forState:UIControlStateNormal];
        rightBtn.backgroundColor = kLYQBlue;
        rightBtn.frame = CGRectMake(0, 0, 62, 30);
        rightBtn.titleLabel.font = kFont(15);
        rightBtn.layer.cornerRadius = 15;
        rightBtn.titleLabel.textColor = [UIColor whiteColor];
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(rightItemAction) forControlEvents:UIControlEventTouchUpInside];
        _rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    }
    return _rightItem;
}

- (LYQHomePublicationView *)publicationView {
    if (_publicationView) {
        return _publicationView;
    }
    
    _publicationView = [[LYQHomePublicationView alloc] init] ;
    _publicationView.alpha = 0;
    _publicationView.hidden = YES;
    
    return _publicationView;
}

#pragma mark - 发布
- (void)rightItemAction {
    
#pragma mark 弹出发布视图
    if (_publicationView.hidden) {
        
        _publicationView.hidden = NO;
        
        [UIView animateWithDuration:0.35 animations:^{
            _publicationView.alpha = 1;
        }];
        
    }
    
}

#pragma mark - textfield delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    //TODO:跳转搜索页面
    if (textField == _searchTxf) {
        UIViewController *view = [[UIViewController alloc] init];
        [self.navigationController pushViewController:view animated:YES];
    }
    
    return NO;
}



#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 3;
    } else if (section == 2) {
        return 4;
    } else {
        return 9+1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        //类型搜索
        LYQHomeAPICell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQHomeAPICell_ID forIndexPath:indexPath];
        return cell;
    } else {
        //标题cell
        if (indexPath.row == 0) {
            LYQHomeSoftCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQHomeSoftCell_ID forIndexPath:indexPath];
            cell.titleLbl.text = _homeSoftTitlesArr[indexPath.section-1];
            return cell;
        } else {
            //旅业商情
            if (indexPath.section == 1) {
                LYQHomeTourismCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQHomeTourismCell_ID forIndexPath:indexPath];
                return cell;
            }
            //热门招聘
            else if (indexPath.section == 2) {
                LYQHomeRecruitCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQHomeRecruitCell_ID forIndexPath:indexPath];
                return cell;
            }
            //最新资源
            else {
                LYQHomeResourceCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQHomeResourceCell_ID forIndexPath:indexPath];
                return cell;
            }
        }
    }
}

#pragma mark - tableview delegate
#pragma mark 自定义头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kWScale(160);
    } else {
        return kWScale(10);
    }
}

#pragma mark 自定义尾视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {    
    return 0.0001f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return kWScale(182.5);
    } else {
        if (indexPath.row == 0) {
            return kWScale(45);
        } else {
            if (indexPath.section == 2) {
                return kWScale(72.5);
            } else if (indexPath.section == 3) {
                return kWScale(120);
            }
        }
                
        return 75;
    }
}

#pragma mark 自定义头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        _bannerHeaderView = [[CXCycleView alloc] initWithFrame:CGRectMake(0, 0, kMS_Width, kWScale(160))];
        _bannerHeaderView.backgroundColor = [UIColor redColor];
        return _bannerHeaderView;
    }
    
    return nil;
}

#pragma mark 设置分割线偏移量
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIEdgeInsets layoutInsets;
    
    if (indexPath.row == 0) {
        layoutInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    } else {
        if (indexPath.section == 1) {
            layoutInsets = UIEdgeInsetsMake(0, kWScale(70), 0, 0);
        } else {
            layoutInsets = UIEdgeInsetsMake(0, kWScale(10), 0, 0);
        }
    }
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:layoutInsets];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:layoutInsets];
    }
}

#pragma mark cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        if (indexPath.section == 1) {
            NSLog(@"跳转到旅业商情");
        } else if (indexPath.section == 2) {
            NSLog(@"跳转到热招职位");
        } else if (indexPath.section == 3) {
            NSLog(@"跳转到最新资源");
        }
    }
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
